package com.demo.dao.impl;

import com.demo.dao.InstructorDao;
import com.demo.dao.StudentDao;
import com.demo.entity.Instructor;
import com.demo.entity.Student;
import com.demo.util.Util;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstructorDaoImpl implements InstructorDao {

    public void add(Instructor vo) {
        //id时自增的不用加
        String sql = "insert into `instructor` (`instructorName`,`id`,`telephone`,`depid`) values(?,?,?,?)";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);
// TODO: 2023/11/11
            ps.setString(1, vo.getInstructorName());
            ps.setLong(2, vo.getId());
            ps.setString(3, vo.getTelephone());
            ps.setString(4, vo.getDepid());
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public void update(Instructor vo) {
        //三个问号要传递三个值
        String sql = "update `instructor` set `instructorName` = ? ,`telephone` = ? ,`depid` = ?   where `id` = ?";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);

            //这不就是三个值吗
            // TODO: 2023/11/11 先 get 再 set
            ps.setString(1, vo.getInstructorName());
            ps.setString(2, vo.getTelephone());
            ps.setString(3, vo.getDepid());
            ps.setLong(4, vo.getId());
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public void delete(long id) {
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "delete from `instructor` where id = " + id;
            s.execute(sql);
            s.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public Instructor get(Serializable id) {
        // TODO: 2023/11/11
        Instructor vo = null;
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "select * from `instructor` where id = " + id;
            ResultSet rs = s.executeQuery(sql);
            if (rs.next()) {
                vo = new Instructor();
                // TODO: 2023/11/11
                vo.setInstructorName(rs.getString("instructorName"));
                vo.setId(rs.getLong("id"));
                vo.setTelephone(rs.getString("telephone"));
                vo.setDepid(rs.getString("depid"));


            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    //@Override
    public Map<String, Object> list(Map<String, Object> params) {
        // TODO: 2023/11/11
        List<Instructor> list = new ArrayList();

        int totalCount = 0;
        String condition = "";
        String sqlList;
        if (params.get("searchColumn") != null && !"".equals(params.get("searchColumn"))) {
            condition += " and `" + params.get("searchColumn") + "` like '%" + params.get("keyword") + "%'";
        }
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps;
            ResultSet rs;
            String limit = (params.get("startIndex") != null && params.get("pageSize") != null) ? " limit " + params.get("startIndex") + "," + params.get("pageSize") : "";
            // TODO: 2023/11/12
            sqlList = "select * from `instructor` where 1=1 " + condition + " order by id asc " + limit + ";";
            ps = c.prepareStatement(sqlList);
            rs = ps.executeQuery();
            while (rs.next()) {

                // TODO: 2023/11/11
                Instructor vo = new Instructor();

                vo.setInstructorName(rs.getString("instructorName"));
                vo.setId(rs.getLong("id"));
                vo.setTelephone(rs.getString("telephone"));
                vo.setDepid(rs.getString("depid"));
                list.add(vo);
            }
            // TODO: 2023/11/11  没有//
            String sqlCount = "select count(*) from `instructor` where 1=1 " + condition;
            ps = c.prepareStatement(sqlCount);
            rs = ps.executeQuery();
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
            rs.close();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap();
        result.put("list", list);
        result.put("totalCount", totalCount);
        return result;
    }
}
